#include "StdAfx.h"
#include "PyFinder.h"
#include "DataParser.h"

PyFinder::PyFinder(void)
{
	setlocale(LC_ALL, "");

	//setlocale(LC_CTYPE, ""); 
	// setlocale(LC_ALL, "zh_CN.UTF-8");       
}

PyFinder::~PyFinder(void)
{
}

void PyFinder::Init()
{
	mCharItems.clear();
	DataParser parser;
	parser.Init(L"py.txt", mCharItems);
}

void PyFinder::Init( char* pData, int size)
{
	mCharItems.clear();
	DataParser parser;
#ifndef _DEBUG
	parser.Init(pData, size, mCharItems);

#endif
}

std::vector<std::wstring> PyFinder::GetFirstPronounce( const std::wstring& str )
{
	std::vector<std::wstring> ret;
	for (std::wstring::const_iterator it(str.begin());
		it != str.end(); ++it)
	{
		ret.push_back(GetShengmu(*it));
	}
	return ret;
}

std::wstring PyFinder::GetShengmu( wchar_t c )
{
	std::wstring ret;
	CharItems::iterator found = mCharItems.find(c);
	if (found != mCharItems.end())
	{
		for (CharItem::iterator itemIt(found->second.begin());
			itemIt != found->second.end(); ++itemIt)
		{
			wchar_t wc = (*itemIt)[0];
			if (ret.find(wc) == std::wstring::npos)
			{
				ret.push_back(wc);
			}
		}
	}
	return ret;
}

bool PyFinder::Check( wchar_t chs, wchar_t shengmu )
{
	return GetShengmu( chs).find(shengmu) != std::wstring::npos;
}